/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.palim.plexdownloader.model.services.ReadWeb;

import ch.palim.plexdownload.jaxb.binder.Data;
import ch.palim.plexdownloader.common.dto.series.EpisodeDTO;
import ch.palim.plexdownloader.common.dto.series.SeasonDTO;
import ch.palim.plexdownloader.common.dto.series.SeriesDTO;
import ch.palim.plexdownloader.common.sevices.Interfaces.SeriesInformation;
import ch.palim.plexdownloader.common.sevices.Interfaces.TheTvDBConnectorLocal;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author Dominik
 */
@Stateless
public class TheTvDBConnector implements TheTvDBConnectorLocal {

    @Override
    public List<SeriesDTO> msGetSeriesInformation(String seriesname) {
        
        //Convert spaces to URL friendly Characters
        seriesname = seriesname.replace(" ", "%20");
        
        Data data = null;
        
        //Call REST Webervice
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http://thetvdb.com/api/GetSeries.php?seriesname=" + seriesname);
            HttpResponse response = client.execute(request);
            
            JAXBContext jaxbContext = JAXBContext.newInstance(Data.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            data = (Data) jaxbUnmarshaller.unmarshal(new StringReader(EntityUtils.toString(response.getEntity())));
            
        } catch (ClientProtocolException ex) {
            Logger.getLogger(TheTvDBConnector.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TheTvDBConnector.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JAXBException ex) {
            Logger.getLogger(TheTvDBConnector.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        if(data != null)
            return convertToSeriesDTO(data.getSeries());
        else
            return null;
    }
    
    @Override
    public List<SeasonDTO> msGetSeriesSeasons(SeriesDTO series) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    public List<EpisodeDTO> msGetSeriesSeasonsEpisodes(SeriesDTO series, SeasonDTO season) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    /**
     * This method converts to the transfer data structure
     * 
     * @param dataSeries
     * @return 
     */
    private List<SeriesDTO> convertToSeriesDTO(List<Data.Series> dataSeries)
    {
        List<SeriesDTO> seriesList = new ArrayList<SeriesDTO>();
        
        Iterator<Data.Series> it = dataSeries.iterator();
        
        while(it.hasNext())
        {
            Data.Series series = it.next();
            
            SeriesDTO dto = new SeriesDTO(series.getSeriesName(), series.getOverview(), "", "", series.getId());
            seriesList.add(dto);
        }
        
        return seriesList;
    }
}
